//
//  ComboBoxiLoL.m
//  ImageReplacer
//
//  Created by Tyler Powell on 5/12/12.
//  Copyright (c) 2012 Georgia Institute of Technology. All rights reserved.
//

#import "ComboBoxiLoL.h"

@implementation ComboBoxiLoL
@synthesize writer;

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self reloadData];
    }
    
    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
}

- (void) awakeFromNib
{
    [self reloadData];
    if ([[writer launchers] count] == 0) {
        return;
    }
    NSString* option = [[writer options] objectAtIndex:0];
    NSInteger placement = [[writer launchers] containsObject:option]?[[writer launchers] indexOfObject:option]:0;
    NSString* string = [[writer launchers] objectAtIndex:placement];
    [self setStringValue:string?string:@""];
    [self display];
}

- (IBAction) removeItem:(id) sender
{
    NSInteger index = [self indexOfSelectedItem];
    if(index < 0){
        NSString* current = [self objectValue];
        index = [[writer launchers] indexOfObject:current];
    }
    //NSLog(@"Tag: %@",index);
    [writer removeLauncher:index];
    if ([[writer launchers] count] > 0) {
        if (index==0) {
            NSString* newLauncher = [[writer launchers] objectAtIndex:0];
            [writer replaceOptionAtIndex:0 withOption:newLauncher];
            [self setStringValue:newLauncher];
        } else {
            NSString* newLauncher = [[writer launchers] objectAtIndex:index-1];
            [writer replaceOptionAtIndex:0 withOption:newLauncher];
            [self setStringValue:newLauncher];
        }
    } else {
        [writer replaceOptionAtIndex:0 withOption:@""];
        [self setStringValue:@""];
    }
}

- (NSInteger) numberOfItemsInComboBox:(NSComboBox*) aComboBox
{
    return [[[aComboBox writer] launchers] count];
}

- (id) comboBox:(NSComboBox*) aComboBox objectValueForItemAtIndex:(NSInteger)index
{
    return [[[aComboBox writer] launchers] objectAtIndex:index];
}

- (unsigned int) comboBox:(NSComboBox*)aComboBox indexOfItemWithStringValue:(NSString *)string
{
    return [[[aComboBox writer] launchers] indexOfObject:string];
}

@end
